﻿using MiniFox.Components;
using MiniFox.Infrastructure;
using MiniFox.Platform.Exceptions;
using Sys.Core.Models.Dto;
using Sys.Core.Models.Queries;
using Sys.Core.Models.VO;
using Sys.Core.Providers;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Sys.Core.Services
{
    [AutoResolve]
    public class DictionaryService : IDictionaryService
    {
        [AutoWired]
        public IDictionaryProvider Provider { get; set; }

        public DictionaryGroupVo Item(string appId, string group)
        {
            return this.Provider.GetItem(appId, group);
        }

        public ResultObject<IEnumerable<DictionaryGroupVo>> Query(string appId, PagedQueryObject<DictionaryQuery> dictionaryQuery)
        {
            return this.Provider.Query(appId, dictionaryQuery);
        }

        public DictionaryGroupVo Create(string appId, DictionaryGroupDto dto)
        {
            if (!this.Provider.Create(appId, dto))
                return null;

            return this.Item(appId, dto.Name);
        }

        public DictionaryGroupVo Update(string appId, DictionaryGroupDto dto)
        {
            if (!this.Provider.Update(appId, dto))
                return null;

            return this.Item(appId, dto.Name);
        }

        public void Remove(string appId, string[] groups)
        {
            if (!this.Provider.Remove(appId, groups))
                throw new PlatformException("ERR_OperateFail");
        }

        public DictionaryGroupVo ToggleItem(string appId, string group, string val)
        {
            if (!this.Provider.Toggle(appId, group, val))
                return null;

            return this.Item(appId, group);
        }
    }
}
